from db_init import db
import sqlalchemy as sa
from datetime import datetime

# class BaseModel(db.Model):
#     query: db.Query
# default=0
# ,defalut=True
#     ...

# 角色与用户多对多
user_role = db.Table(
    'user_role',
    db.Column('role_id',db.ForeignKey('role.id'),primary_key=True),
    db.Column('user_id',db.ForeignKey('user.id'),primary_key=True)
)

# 用户表
class User(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    img = db.Column(db.String(1024),default='https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png')
    name = db.Column(db.String(16))
    password = db.Column(db.String(16))
    mobile = db.Column(db.String(16))
    money = db.Column(sa.DECIMAL(12,2))
    # 和购物测和绑定的外键
    user = db.relationship('Keeping',backref='user')
    # 和优惠卷帮党的外键
    user1 = db.relationship('Coupon',backref='user')
    #与签到表关联
    check = db.relationship('CheckIn',uselist=False,backref='user')
    #与角色表进行关联
    role_id = db.relationship('Role', secondary=user_role, backref=db.backref('user'))

# 角色表
class Role(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(16))

role_power = db.Table(
    'role_power',
    db.Column('role_id',db.ForeignKey('role.id'),primary_key=True),
    db.Column('power_id',db.ForeignKey('power.id'),primary_key=True)
)

# 权限表
class Power(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(16))
    role = db.relationship('Role',secondary=role_power,backref=db.backref('power'))
    url = db.Column(db.String(1024))
    method = db.Column(db.String(16))

# 签到表
class CheckIn(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    status = db.Column(db.Boolean,default=0)
    check_data = db.Column(db.DATE)

#优惠券
class Coupon(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    c_name = db.Column(db.String(16))
    price = db.Column(db.String(16))
    start_date = db.Column(db.DateTime,default=datetime.now().strftime('%Y-%m-%d'))
    end_data = db.Column(db.DateTime)
    # 优惠卷绑定的用户id
    user_id = db.Column(db.Integer,db.ForeignKey('user.id'))

# 购物车
class Keeping(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    k_name = db.Column(db.String(16))
    price = db.Column(db.String(16))
    num = db.Column(db.String(16))
    goods_h = db.Column(db.String(16))
    user_id = db.Column(db.Integer,db.ForeignKey('user.id'))

# 小料和商品多对多关系
food_goods = db.Table(
    'food_goods',
    db.Column('food_id',db.ForeignKey('goods.id'),primary_key=True),
    db.Column('goods_id',db.ForeignKey('food.id'),primary_key=True)
)

# 类别表
class Cate(db.Model):
    # 商品id
    id = db.Column(db.Integer,primary_key=True)
    # 商家id
    c_name = db.Column(db.String(16))
    # 商品id
    cate = db.relationship('Goods',backref='cate')

# 商品表
class Goods(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    # 商品名称
    g_name = db.Column(db.String(16))
    # 库存
    count_num = db.Column(db.String(16))
    price = db.Column(db.Integer)
    img = db.Column(db.String(2048),default='https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png')
    # 商品简介
    countent = db.Column(db.String(100))
    # 商品和种类的一对多
    cate_id = db.Column(db.Integer,db.ForeignKey('cate.id'))
    # 商品和成品的一对多关系
    goods = db.relationship('Goofiness', backref='goods')
    # 商家和商品的关系
    goods_id = db.Column(db.Integer,db.ForeignKey('goods_h.id'))
    food_id = db.relationship('Food',secondary=food_goods,backref=db.backref('food'),lazy='dynamic')

# 商家和商品的多对多关系
goods_goods_h = db.Table(
    'goods_goods_h',
    db.Column('goods_id',db.ForeignKey('goods.id'),primary_key=True),
    db.Column('goods_h_id',db.ForeignKey('goods_h.id'),primary_key=True),
    db.Column('num',db.INTEGER,nullable=True,default=True)
)

# 商家表
class Goods_H(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    g_name = db.Column(db.String(16))
    g_addr = db.Column(db.String(102))
    is_defalue = db.Column(db.Boolean)
    order = db.relationship('Order', backref='goods_h')
    #商户和商品的多对多关系表
    goods_h_id = db.relationship('Goods_H', secondary=goods_goods_h, backref=db.backref('goods_h'),)

#订单表
class Order(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    # 订单的状态
    status = db.Column(db.String(16))
    # 总计
    goods_price = db.Column(db.String(1024))
    # 总价
    count_num = db.Column(db.String(1024))
    # 订单的内容
    countent = db.Column(db.String(100))
    # 订单的编号
    number = db.Column(db.String(100))
    # 订单的备注
    remarks = db.Column(db.String(100),default='')
    # 订单和成品的关系
    goofiness_id = db.relationship('Goofiness', backref='goofiness.id')
    # 订单和商家的一对多关系
    goods_h_id = db.Column(db.Integer, db.ForeignKey('goods_h.id'))

# 小料和成品
food_goofiness = db.Table(
    'food_goofiness',
    db.Column('food_id',db.ForeignKey('food.id'),primary_key=True),
    db.Column('goofiness_id',db.ForeignKey('goofiness.id'),primary_key=True)
)

# 成品表
class Goofiness(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    order = db.Column(db.Integer, db.ForeignKey('order.id'))
    food_id = db.relationship('Food', secondary=food_goofiness, backref=db.backref('goofiness'), lazy='dynamic')
    goods_id = db.Column(db.Integer, db.ForeignKey('goods.id'))

# 小料表
class Food(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(16))
    price = db.Column(db.String(16))

# 用户日志表
class User_log(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    url = db.Column(db.String(1024))
    methods = db.Column(db.String(32))
    create_time = db.Column(db.Integer)


